//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    fileServerPath: app.globalData.fileServerPath,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    loadingHidden: false , // loading
    userInfo: {},
    swiperCurrent: 0,  
    selectCurrent:0,
    categories: [],
    activeCategoryId: 0,
    goods:[],
    banners:[],
    scrollTop:"0",
    loadingMoreHidden:true,
    currentPage: 1,
    totalPage:0,
    pageSize: 10,
    hasNoCoupons:true,
    coupons: []
  },
  onLoad: function () {
    var that = this;
    wx.request({
      url: app.globalData.rootPath + '/m/banner',
      success: function (res) {
        if (res.data.meta.code == 200) {
          that.setData({
            banners: res.data.data,
            activeCategoryId: 0
          });
        }
      }
    })
    wx.request({
      url: app.globalData.rootPath + '/m/category/all',
      success: function (res) {
        var categories = [{ id: 0, name: "热门" }];
        if (res.data.meta.code == 200) {
          for (var i = 0; i < res.data.data.length; i++) {
            categories.push(res.data.data[i]);
          }
        }
        that.setData({
          categories: categories,
          activeCategoryId: 0,

        });
        that.getGoodsList(0, that.data.currentPage, that.data.pageSize);
      }
    })

  },
  //点击类别获取id请求不同的商品数据
  tabClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id,
      currentPage: 1,
      totalPage: 0,
    });
    this.getGoodsList(this.data.activeCategoryId, this.data.currentPage, this.data.pageSize);
  },
  //事件处理函数
  swiperchange: function(e) {
       this.setData({  
        swiperCurrent: e.detail.current  
    })  
  },
  
  toDetailsTap:function(e){
    wx.navigateTo({
      url:"/pages/goods-details/index?id="+e.currentTarget.dataset.id
    })
  },
  bindTypeTap: function(e) {
     this.setData({  
        selectCurrent: e.index  
    })  
  },
  scroll: function (e) {
    var that = this,scrollTop=that.data.scrollTop;
    that.setData({
      scrollTop:e.detail.scrollTop
    })
    
  },
  loadMore: function () {
    var that = this;
    // 当前页+1
    if (that.data.currentPage < that.data.totalPage) {
      that.getGoodsList(that.data.activeCategoryId, that.data.currentPage+1, that.data.pageSize)
    }else{
      that.setData({
        loadingMoreHidden:false
      })
    }
  },
  
  getGoodsList: function (categoryId, currentPage, pageSize) {
    var that = this;
    that.setData({
      goods: [],
    });
    if (categoryId == 0) {
      wx.request({
        url: app.globalData.rootPath + '/m/goods/hot',
        data: {
          cat_id: categoryId,
          currentPage: currentPage,
          pageSize: pageSize,
        },
        success: function (res) {
          var goods = [];
          if (res.data.meta.code === 200) {
            if (res.data.data.list.length > 0) {
              for (var i = 0; i < res.data.data.list.length; i++) {
                goods.push(res.data.data.list[i]);
              }
              if (res.data.data.currentPage < res.data.data.totalPage) {
                that.setData({
                  goods: goods,
                  currentPage: res.data.data.currentPage,
                  totalPage: res.data.data.totalPage,
                  loadingMoreHidden: false,
                });
              } else {
                that.setData({
                  goods: goods,
                  currentPage: res.data.data.currentPage,
                  totalPage: res.data.data.totalPage,
                  loadingMoreHidden: true,
                });
              }
            } else {
              return;
            }
          } else {
            wx.showModal({
              title: '提示',
              content: '请求出错--' + res.data.meta.code,
              showCancel: false
            })
          }
        }
      })
    } else {
      wx.request({
        url: app.globalData.rootPath + '/m/goods/cat',
        data: {
          cat_id: categoryId,
          currentPage: currentPage,
          pageSize: pageSize,
        },
        success: function (res) {
          var goods = [];
          if (res.data.data.list.length == 0) {
            that.setData({
              loadingMoreHidden: true,
            });
            return;
          }
          for (var i = 0; i < res.data.data.list.length; i++) {
            goods.push(res.data.data.list[i]);
          }
          if (res.data.data.currentPage < res.data.data.totalPage) {
            that.setData({
              goods: goods,
              currentPage: res.data.data.currentPage,
              totalPage: res.data.data.totalPage,
              loadingMoreHidden: false,
            });
          } else {
            that.setData({
              goods: goods,
              currentPage: res.data.data.currentPage,
              totalPage: res.data.data.totalPage,
              loadingMoreHidden: true,
            });
          }
        }
      })
    }
  },
  /*
  getCoupons: function () {
    var that = this;
    wx.request({
      url: 'https://api.it120.cc/' + app.globalData.subDomain + '/discounts/coupons',
      data: {
        type: ''
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            hasNoCoupons: false,
            coupons: res.data.data
          });
        }
      }
    })
  },
  gitCoupon : function (e) {
    var that = this;
    wx.request({
      url: 'https://api.it120.cc/' + app.globalData.subDomain + '/discounts/fetch',
      data: {
        id: e.currentTarget.dataset.id,
        token: app.globalData.token
      },
      success: function (res) {
        if (res.data.code == 20001 || res.data.code == 20002) {
          wx.showModal({
            title: '错误',
            content: '来晚了',
            showCancel: false
          })
          return;
        }
        if (res.data.code == 20003) {
          wx.showModal({
            title: '错误',
            content: '你领过了，别贪心哦~',
            showCancel: false
          })
          return;
        }
        if (res.data.code == 20004) {
          wx.showModal({
            title: '错误',
            content: '已过期~',
            showCancel: false
          })
          return;
        }
        if (res.data.code == 0) {
          wx.showToast({
            title: '领取成功，赶紧去下单吧~',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showModal({
            title: '错误',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })
  },
  */
  onShareAppMessage: function () {
    return {
      title: wx.getStorageSync('mallName'),
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
  
})
